// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.IO;
using System.Xml;
using svs.Mobile.BeyondMedia.FeedCore;
using svs.Mobile.BeyondMedia.FeedCore.RSS.Parsers;

namespace svs.Mobile.BeyondMedia.FeedCore.RSS
{
	public class OpmlManager
	{
		public static Feed LoadOpml(string file)
		{
			Feed retVal = null;

			if (string.IsNullOrEmpty(file))
				throw new ApplicationException("The OPML Path is invalid!");

			lock (file)
			{
				if (File.Exists(file))
				{
					XmlReader tr = null;
					try
					{
						tr = RssFeedCache.CreateReader(file);

						OpmlFeedParser pb = new OpmlFeedParser();

						while (tr.Read())
						{
							//Handle the XML
							pb.Handle(tr);
						}

						retVal = pb.Feed;
					}
					catch (Exception ex)
					{
						CoreHelper.WriteLogEntry("Unable to read the OPML data for feed " + file + " Error:" +
						                         ex.Message);
						throw new ApplicationException(
							string.Format(
								"File: '{0}' is not a valid OPML file!", file));
					}
					finally
					{
						if (tr != null)
							tr.Close();
					}
				}
			}

			return retVal;
		}
	}
}